<?php
session_start();
//ini_set('display_errors',1);
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/database.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/functions.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/class.admin.php');
$admin=new admin();
$admin->canView($_SERVER['PHP_SELF']);
//print_r($_POST);
$group_name=$admin->clean($_POST['group_name']);
$group_id=$_POST['group_id'];
$group_functions=$_POST['group_functionality'];
$group_pages=$_POST['group_page'];

$data=new database();
$data->db(DATABASE)->collection('admin_pages');
$data->criteria(array('$pull' => array("groups" => $group_id)));
$data->updateMultiple(array('path'=>array('$exists'=>true)));
$data->close();

if(count($group_functions)>=1) {
$data=new database();
$data->db(DATABASE)->collection('admin_pages');

foreach($group_functions as $function) {
$data->criteria(array('$addToSet' => array("groups" => $group_id)));
$data->updateMultiple(array('path'=>$function));
}
$data->close();

}
// PULL FROM EVERYWHERE FIRST
if(count($group_pages)>=1) {
$data=new database();
$data->db(DATABASE)->collection('admin_pages');

foreach($group_pages as $key=>$val) {
$data->criteria(array('$addToSet' => array("groups" => $group_id)));
$data->updateMultiple(array('path'=>$key,'role'=>'page','name'=>$val));
}
$data->close();
}


?>
<script>
$.fn.message('show','<div class="ok_h_message"><h2>Group updated</h2><p><strong>The group settings have been updated</strong></p></div>');</script>
